const a = document.getElementById('addBook')
const addDialog = document.getElementsByClassName('addBook-wrapper')[0]

const updateDialog = document.getElementsByClassName('updateBook-wrapper')[0]
const updateBtn= document.getElementById('updateBtn')



const searchBookInput = document.getElementsByClassName("searchBookText")[0];
const searchBookBtn = document.getElementsByClassName("searchBookBtn")[0];



a.addEventListener('click',()=>{
    addDialog.style.display = 'block'
})


var cancleBtn = document.getElementsByClassName('cancle');
cancleBtn[0].addEventListener('click',()=>{
    addDialog.style.display = 'none';
});

cancleBtn[1].addEventListener('click',()=>{
    updateDialog.style.display = 'none';
});





updateBtn.addEventListener('click',()=>{
    updateDialog.style.display = 'none'
})


searchBookBtn.addEventListener('click',()=>{
    var searchBookText = searchBookInput.value
    location.href = `http://localhost:8080/library/admin/bookManage?searchBookText=${searchBookText}`;

});


const deleteUserBtn = document.getElementsByClassName('delete');
for (let i=0;i<deleteUserBtn.length;i++){
    let bid = deleteUserBtn[i].getAttribute('bid');
    deleteUserBtn[i].addEventListener('click',()=>{
        if(confirm(`是否删除id为${bid}的图书`)){
            location.href = `http://localhost:8080/library/admin/deleteBook?bid=${bid}`;
            alert("删除成功！");
        }
        return false;
    });
}


const updateAs = document.getElementsByClassName('update')
for(let i=0;i<updateAs.length;i++){

    updateAs[i].addEventListener('click',()=>{


        let bid = updateAs[i].getAttribute('bid');
        let author = updateAs[i].getAttribute('author');
        let name = updateAs[i].getAttribute('name');
        let num = updateAs[i].getAttribute('num');
        let remainder = updateAs[i].getAttribute('remainder');
        let press = updateAs[i].getAttribute('press');
        let address = updateAs[i].getAttribute('address');


        updateDialog.style.display = 'block';
        var updateInputs = document.querySelectorAll("#updateForm input")
        updateInputs[0].value =  bid;
        updateInputs[0].setAttribute("disabled","disabled");
        updateInputs[1].value =  name;
        updateInputs[2].value = author;
        updateInputs[3].value = num;
        updateInputs[4].value = remainder;
        updateInputs[5].value = address;
        updateInputs[6].value = press;

        return false;
    });
}